package com.jishuo.base.config;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 系统参数
 */
@Configuration
public class AppConfig {
    /* 加密的配置 */
    public static final String DECODEKEY = "ff2f10b642ad44aca3c49f8a16c526f4ff2f";
    /* 图像验证码session key */
    public static final String CAPTCHA_SESSION_KEY = "validate_code";
    /* 登录用户session key */
    public static final String LOGIN_USER_SESSION_KEY = "login_user";
    /* 自定义错误编码 */
    public static final String CUSTOM_ERR = "S9100";

    @Bean("jasyptStringEncryptor")
    public StringEncryptor jasyptStringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("NonTax2024");
        config.setAlgorithm("PBEWithMD5AndDES");
//        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
//        config.setProviderName("SunJCE");
//        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
//        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
//        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }

    public static void main(String[] args) throws Exception {
        String message = "123456";
        String password = "NonTax2024";

        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("NonTax2024");
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPoolSize("1");
        encryptor.setConfig(config);
        //一个同样的密码和秘钥，每次执行加密，密文都是不一样的。但是解密是没问题的。
        String jasyptEncrypt = encryptor.encrypt(message);
        System.out.println(jasyptEncrypt);

        String jasyptEncrypt1 = encryptor.decrypt(jasyptEncrypt); // stringEncryptor(password, "UVgGJ4BAS8XrVNsamg60RO0ADCzdCKuR2+bvINv7Wyej3YTqVmxp5g1", false);
        System.out.println(jasyptEncrypt1);
    }

}
